/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { jsPDF } from "./img2pdf/index.js"
import {getDocument} from "./pdfjs/display/api.js"
import { image } from "./image.js";
let runTime = 200;
  //options：format : a4纸张，orientation:纵向，单位为毫米
  let options = {
    orientation: "p",
    unit: "pt",
    format: "a4",
    floatPrecision: 2
}
function jsPDFText() {
    let arr = new Array(10);
    let len = arr.length;
    let start = Date.now();
    for (let i = 0; i < 50; i++) {
        arr[i % len] = new jsPDF(options);
    }
    let end = Date.now();
    print("pdfview_new: " + (end - start) + ' ms')
    for (let i = 0; i < 10; i++) {
		if (arr[i] == null) {
			 print("test fail")
		}
	}
}

function addImageTest() {
    let arr = new Array(10);
    let len = arr.length;
    let jsPdf = new jsPDF(options);
    let start = Date.now();
    for (let i = 0; i < 50; i++) {
        arr[i % len] = jsPdf.addImage(image, 100, 100, 200, 200);
    }
    let end = Date.now();
    print("pdfview_addImage: " + (end - start) + ' ms')
    for (let i = 0; i < 10; i++) {
		if (arr[i] == null) {
			 print("test fail")
		}
	}
}
function buildDocumentTest() {
    let arr = new Array(10);
    let len = arr.length;
    let jsPdf = new jsPDF(options);
    jsPdf.addImage(image, 100, 100, 200, 200)
    let start = Date.now();
    for (let i = 0; i < 1000; i++) {
        arr[i % len] = jsPdf.buildDocument();
    }
    let end = Date.now();
    print("pdfview_buildDocument: " + (end - start) + ' ms')
    for (let i = 0; i < 10; i++) {
		if (arr[i] == null) {
			 print("test fail")
		}
	}
}
function getArrayBufferTest() {
    let arr = new Array(10);
    let len = arr.length;
    let jsPdf = new jsPDF(options);
    jsPdf.addImage(image, 100, 100, 200, 200)
    let content = jsPdf.buildDocument()
    let start = Date.now();
    for (let i = 0; i < 10; i++) {
        arr[i % len] = jsPdf.getArrayBuffer(content);
    }
    let end = Date.now();
    print("pdfview_getArrayBuffer: " + (end - start) + ' ms')
    for (let i = 0; i < 10; i++) {
		if (arr[i] == null) {
			 print("test fail")
		}
	}
}
function addPageTest() {
    let arr = new Array(10);
    let len = arr.length;
    let jsPdf = new jsPDF(options);
    let start = Date.now();
    for (let i = 0; i < 5000; i++) {
        arr[i % len] = jsPdf.addPage();
    }
    let end = Date.now();
    print("pdfview_addPage: " + (end - start) + ' ms')
    for (let i = 0; i < 10; i++) {
		if (arr[i] == null) {
			 print("test fail")
		}
	}
}
function movePageTest() {
    let arr = new Array(10);
    let len = arr.length;
    let jsPdf = new jsPDF(options);
    jsPdf.addPage();
    jsPdf.addPage();
    let start = Date.now();
    for (let i = 0; i < 50000; i++) {
        arr[i % len] = jsPdf.movePage(1,2);
    }
    let end = Date.now();
    print("pdfview_movePage: " + (end - start) + ' ms')
    for (let i = 0; i < 10; i++) {
		if (arr[i] == null) {
			 print("test fail")
		}
	}
}
function setPageTest() {
    let arr = new Array(10);
    let len = arr.length;
    let jsPdf = new jsPDF(options);
    let start = Date.now();
    for (let i = 0; i < 20000; i++) {
        arr[i % len] = jsPdf.setPage(1);
    }
    let end = Date.now();
    print("pdfview_setPage: " + (end - start) + ' ms')
    for (let i = 0; i < 10; i++) {
		if (arr[i] == null) {
			 print("test fail")
		}
	}
}
function insertPageTest() {
    let arr = new Array(10);
    let len = arr.length;
    let jsPdf = new jsPDF(options);
    let start = Date.now();
    for (let i = 0; i < 2000; i++) {
        arr[i % len] = jsPdf.insertPage(1);
    }
    let end = Date.now();
    print("pdfview_insertPage: " + (end - start) + ' ms')
    for (let i = 0; i < 10; i++) {
		if (arr[i] == null) {
			 print("test fail")
		}
	}
}
function textTest() {
    let arr = new Array(10);
    let len = arr.length;
    let jsPdf = new jsPDF(options);
    let start = Date.now();
    for (let i = 0; i < runTime; i++) {
        arr[i % len] = jsPdf.text("you die,i life",0,0);
    }
    let end = Date.now();
    print("pdfview_textTest: " + (end - start) + ' ms')
    for (let i = 0; i < 10; i++) {
		if (arr[i] == null) {
			 print("test fail")
		}
	}
}
function outputTest() {
    let arr = new Array(10);
    let len = arr.length;
    let jsPdf = new jsPDF(options);
    let start = Date.now();
    for (let i = 0; i < 100; i++) {
        arr[i % len] = jsPdf.output("dataurlstring");
    }
    let end = Date.now();
    print("pdfview_output: " + (end - start) + ' ms')
    for (let i = 0; i < 10; i++) {
		if (arr[i] == null) {
			 print("test fail")
		}
	}
}
function getDocumentTest() {
    let arr = new Array(10);
    let len = arr.length;
    let jsPdf = new jsPDF();
    jsPdf.addImage(image, 100, 100, 200, 200);
    jsPdf.text("you are good,i am  feel hot.",100,100)
    let content = jsPdf.buildDocument();
    let buf = jsPdf.getArrayBuffer(content)
    let start = Date.now();
    for (let i = 0; i < 2000; i++) {
        arr[i % len] = getDocument(buf);
    }
    let end = Date.now();
    print("pdfview_getDocument: " + (end - start) + ' ms')
    for (let i = 0; i < 10; i++) {
		if (arr[i] == null) {
			 print("test fail")
		}
	}
}

let loopCountForPreheat = 1;
for (let i = 0; i < loopCountForPreheat; i++) {
    jsPDFText()
    addImageTest()
    buildDocumentTest()
    getArrayBufferTest()
    addPageTest()
    movePageTest()
    setPageTest()
    insertPageTest()
    textTest()
    outputTest()
    getDocumentTest()
}

ArkTools.waitAllJitCompileFinish();

jsPDFText()
addImageTest()
buildDocumentTest()
getArrayBufferTest()
addPageTest()
movePageTest()
setPageTest()
insertPageTest()
textTest()
outputTest()
getDocumentTest()